﻿using System.Linq.Expressions;
using LF.Domain.Entities;
using LF.Domain.Helper;

namespace LF.Domain.Services.Iml
{
    public class GlobalManagerService : IGlobalManagerService
    {
        public long Count<T>(Expression<Func<T, bool>>? where = null) where T : BaseEntity
        {
            var query = FreeSqlHelper.DbContext.Queryable<T>();
            if (where != null)
            {
                query = query.Where(where);
            }

            return query.Count();
        }

        public bool Delete<T>(Guid id) where T : BaseEntity
        {
            var rsult = FreeSqlHelper.DbContext.Update<T>(id).Set(p => p.IsDelete == true).ExecuteAffrows();
            return rsult > 0;
        }

        public bool DeleteByWhere<T>(Expression<Func<T, bool>>? where = null) where T : BaseEntity
        {
            var rsult = FreeSqlHelper.DbContext.Update<T>()
                .Set(p => p.IsDelete == true)
                .Where(where)
                .ExecuteAffrows();
            return rsult > 0;
        }

        public T Get<T>(Guid id) where T : BaseEntity
        {
            var item = FreeSqlHelper.DbContext.Queryable<T>().Where(p => p.Id == id).First();
            return item;
        }

        public T Get<T>(Expression<Func<T, bool>>? where = null) where T : BaseEntity
        {
            var query = FreeSqlHelper.DbContext.Queryable<T>();
            if (where != null)
            {
                query = query.Where(where);
            }
            var item = query.First();
            return item;
        }

        public bool Insert<T>(T t) where T : BaseEntity
        {
            var result = FreeSqlHelper.DbContext.Insert(t).ExecuteAffrows();
            return result > 0;
        }

        public bool Insert<T>(IEnumerable<T> t) where T : BaseEntity
        {
            var result = FreeSqlHelper.DbContext.Insert(t).ExecuteAffrows();
            return result > 0;
        }

        public bool RealDelete<T>(Guid id) where T : BaseEntity
        {
            var result = FreeSqlHelper.DbContext.Delete<T>(id).ExecuteAffrows();
            return result > 0;
        }

        public bool RealDeleteByWhere<T>(Expression<Func<T, bool>>? where = null) where T : BaseEntity
        {
            var result = FreeSqlHelper.DbContext.Delete<T>().Where(where).ExecuteAffrows();
            return result > 0;
        }

        public List<T> ToList<T>(Expression<Func<T, bool>>? where = null) where T : BaseEntity
        {
            var query = FreeSqlHelper.DbContext.Queryable<T>();
            if (where != null)
            {
                query = query.Where(where);
            }
            return query.ToList();
        }

        public List<T> ToPageList<T>( int index, int pageSize, Expression<Func<T, bool>>? where = null) where T : BaseEntity
        {
            var query = FreeSqlHelper.DbContext.Queryable<T>();
            if (where != null)
            {
                query = query.Where(where);
            }
            var list = query
                .OrderBy(p => p.UpdateTime)
                .Page(index, pageSize)
                .ToList();
            return list;
        }

        public List<T> ToPageList<T,TMember>(int index, int pageSize, Expression<Func<T, TMember>> order, Expression<Func<T, bool>>? where = null) where T : BaseEntity
        {
            var query = FreeSqlHelper.DbContext.Queryable<T>();
            if (where != null)
            {
                query = query.Where(where);
            }
            var list = query
                .OrderBy(order)
                .Page(index, pageSize)
                .ToList();
            return list;
        }

        public List<T> ToPageListDesc<T,TMember>( int index, int pageSize, Expression<Func<T, TMember>> order, Expression<Func<T, bool>>? where = null) where T : BaseEntity
        {
            var query = FreeSqlHelper.DbContext.Queryable<T>();
            if (where != null)
            {
                query = query.Where(where);
            }
            var list = query
                .OrderByDescending(order)
                .Page(index, pageSize)
                .ToList();
            return list;
        }

        public bool Update<T>(T t) where T : BaseEntity
        {
            var result = FreeSqlHelper.DbContext.Update<T>(t.Id)
              .SetSource(t)
              .ExecuteAffrows();
            return result > 0;
        }
    }
}

